package com.blueberry.quiz_master.quiz.controller;

import com.blueberry.quiz_master.common.Result;
import com.blueberry.quiz_master.quiz.model.QuestionBankRequest;
import com.blueberry.quiz_master.quiz.model.QuestionBankResponse;
import com.blueberry.quiz_master.quiz.service.QuestionBankService;
import com.blueberry.quiz_master.task.Task;
import com.blueberry.quiz_master.task.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("question-bank")
public class QuestionBankController {
    @Autowired
    private QuestionBankService questionBankService;
    @Autowired
    private TaskService taskService;

    /**
     * 提交题库生成任务
     */
    @PostMapping("/generate")
    public Result<String> generateQuestionBank(@RequestBody QuestionBankRequest request) {
        String taskId = questionBankService.submitGenerateTask(request);
        return Result.success(taskId);  // 返回任务ID，供查询状态
    }

    /**
     * 查询任务状态
     */
    @GetMapping("/task/status")
    public Result<Task> queryTaskStatus(@RequestParam String taskId) {
        Task task = taskService.getTaskById(taskId);
        if (task == null) {
            return Result.fail("任务不存在");
        }
        return Result.success(task);
    }

    /**
     * 查询题库详情
     */
    @GetMapping("/{id}")
    public Result<QuestionBankResponse> getQuestionBank(@PathVariable String id) {
        QuestionBankResponse response = questionBankService.getQuestionBank(id);
        if (response == null) {
            return Result.fail("题库不存在");
        }
        return Result.success(response);
    }
}
